using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteAlways]
public class Creature : MonoBehaviour {
	public string entityTag;
	private Grid m_grid;
	private static List<Creature> m_entities = new();
	public static List<Creature> gridEntities {
		get {
			m_entities.RemoveAll(x => x.IsNullOrUObjectNull());
			return m_entities;
		}
	}

	public void Awake() {
		if (!Application.isPlaying) {
			return;
		}

		m_entities.Add(this);
	}

	private void Update() {
		if (Application.isPlaying) {
			return;
		}
		if (m_grid == null) {
			m_grid = GetComponentInParent<Grid>();
		}
		if (m_grid == null) {
			return;
		}
		var gridPos = m_grid.WorldToCell(transform.position);
		var halfGrid = m_grid.CellToWorld(Vector3Int.one) / 2;
		var aligned = m_grid.CellToWorld(gridPos) + halfGrid;
		transform.position = aligned;
	}
}
